import storage from 'good-storage';

// search
const SEARCH_KEY = '__search__';
const SEARCH_MAX_LEN = 10;

// play
const PLAY_KEY = '__play__';
const PLAY_MAX_LEN = 200;

// favorite
const FAVORITE_KEY = '__favorite__';
const FAVORITE_MAX_LEN = 200;

/**
 * 向数组中插入一条记录
 * @param {Array} arr 目标数组
 * @param {Object | String} val 待插入的值
 * @param {Function} compare findIndex()的回调函数
 * @param {Number} maxLen 最大存储长度
 * @returns 
 */
function insertArray(arr, val, compare, maxLen) {
  // 查找与插入元素相同的数组元素的索引
  const index = arr.findIndex(compare);
  // 如果当前数组只有这一项，直接返回
  if (index === 0) {
    return;
  }
  // 如果有这一项先删除这一项
  if (index > 0) {
    arr.splice(index, 1);
  }
  // 把新元素插入数组前端
  arr.unshift(val);
  // 如果当前素注射元素个数超过20，把数组最早的元素删除
  if (maxLen && arr.length > maxLen) {
    arr.pop();
  }
}

/**
 * 从数组中删除记录
 * @param {Array} arr 目标数组
 * @param {Function} compare findIndex()的回调函数
 */
function deleteFromArray(arr, compare) {
  const index = arr.findIndex(compare);
  if (index > -1) {
    arr.splice(index, 1);
  }
}

/**
 * 保存搜索记录
 * @param {String} query 查询内容
 * @returns 
 */
export function saveSearch(query) {
  let searches = storage.get(SEARCH_KEY, []);
  insertArray(
    searches,
    query,
    item => {
      return item === query;
    },
    SEARCH_MAX_LEN
  );
  storage.set(SEARCH_KEY, searches);
  return searches;
}

/**
 * 获取搜索记录
 * @returns 
 */
export function loadSearch() {
  return storage.get(SEARCH_KEY, []);
}

/**
 * 删除搜索记录
 * @param {String} query 目标元素
 * @returns 
 */
export function deleteSearch(query) {
  let searches = storage.get(SEARCH_KEY, []);
  deleteFromArray(searches, item => {
    return item === query;
  });
  storage.set(SEARCH_KEY, searches);
  return searches;
}

/**
 * 清空搜索记录
 * @returns 
 */
export function clearSearch() {
  storage.remove(SEARCH_KEY);
  return [];
}

/**
 * 保存播放记录
 * @param {Object} song 歌曲对象
 * @returns 
 */
export function savePlay(song) {
  let songs = storage.get(PLAY_KEY, []);
  insertArray(
    songs,
    song,
    item => {
      return song.id === item.id;
    },
    PLAY_MAX_LEN
  );
  storage.set(PLAY_KEY, songs);
  return songs;
}

/**
 * 获取播放记录
 * @returns 
 */
export function loadPlay() {
  return storage.get(PLAY_KEY, []);
}

/**
 * 删除播放记录
 * @param {Object} song 歌曲对象
 * @returns 
 */
export function deletePlay(song) {
  let songs = storage.get(PLAY_KEY, []);
  deleteFromArray(songs, item => {
    return item.id === song.id;
  });
  storage.set(PLAY_KEY, songs);
  return songs;
}

/**
 * 清空播放记录
 * @returns 
 */
export function clearPlay() {
  storage.remove(PLAY_KEY);
  return songs;
}

/**
 * 添加至我喜欢（收藏）
 * @param {Object} song 歌曲对象
 * @returns 
 */
export function saveFavorite(song) {
  // 保存收藏
  let songs = storage.get(FAVORITE_KEY, []);
  insertArray(
    songs,
    song,
    item => {
      return song.id === item.id;
    },
    FAVORITE_MAX_LEN
  );
  storage.set(FAVORITE_KEY, songs);
  return songs;
}

/**
 * 获取收藏记录
 * @returns 
 */
export function loadFavorite() {
  return storage.get(FAVORITE_KEY, []);
}

/**
 * 取消收藏
 * @param {Object} song 歌曲对象
 * @returns 
 */
export function deleteFavorite(song) {
  let songs = storage.get(FAVORITE_KEY, []);
  deleteFromArray(songs, item => {
    return item.id === song.id;
  });
  storage.set(FAVORITE_KEY, songs);
  return songs;
}

/**
 * 清空收藏记录
 * @returns 
 */
export function clearFavorite() {
  storage.remove(FAVORITE_KEY);
  return [];
}


